// order//order/labelManage/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classList:[],
    showDialog: false,
    labelName: '',
    labelOrder: '',
    id: 0
  },

  addLabel: function(){
    this.setData({
      id: 0,
      showDialog: !this.data.showDialog
    })
  },
  closedialog: function(){
    this.setData({
      showDialog: !this.data.showDialog
    })
  },
  bindName: function (e) {
    this.setData({
      labelName: e.detail.value
    })
  },
  bindOrder: function(e){
    this.setData({
      labelOrder: e.detail.value
    })
  },
  addLabelConfirm: function(){
     //新增商品分类
    let that = this;
    let labelName = that.data.labelName;
    let displayOrder = that.data.labelOrder;
    if(!displayOrder){
      displayOrder = 0;
    }
    if (!labelName) {
      wx.showModal({
        title: '提示',
        content: '请填写标签名称',
        showCancel: false,
      })
      return;
    }
    that.closedialog();
    let StoreCid = that.data.id;
    let storeclass = 'CreateStoreClass';
    if(StoreCid > 0){
      storeclass = 'UpdateStoreClass';
    }
    let store = {
      "StoreCid": StoreCid,
      "StoreId": app.globalData.storeId,
      "DisplayOrder": displayOrder,
      "Name": labelName,
      "ParentId": 0,
      "Layer": 0,
      "HasChild": 0,
      "Path": "",
      "Logo": "",
      "Uid": app.globalData.userInfo.Uid
    }
    wx.request({
      url: app.globalData.host + '/api/Store/' + storeclass,
      data: store,
      method: 'POST',
      success: function (res) {
        if (res.data.Code == 0) {
          wx.showToast({
            title: '新增成功',
            mask: true,
          })
          that.getClassList();
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false,
          })
        }
      },
    })
  },
  editor: function(e){
    let id = e.currentTarget.dataset.id;
    let name = e.currentTarget.dataset.name;
    let order = e.currentTarget.dataset.order;
    this.setData({
      showDialog: !this.data.showDialog,
      labelOrder: order,
      labelName: name,
      id: id
    })
  },
  delete: function(e){
    var that = this;
    var cid = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否确认删除？',
      success: function(ress){
        if(ress.confirm){
          wx.request({
            url: app.globalData.host + '/api/Store/DeleteStoreClass?storeCid=' + cid + '&uid=' + app.globalData.userInfo.Uid,
            success: function (res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '删除成功',
                  mask: true,
                })
                that.getClassList();
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false,
                })
              }
            },
          })
        }
      }
    })
  },

  // 产品分类
  getClassList: function(){
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Store/GetStoreClassList?storeId=' + app.globalData.storeId + "&uid=" + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            var list = res.data.Data;
            var newList = [];
            var other = [];
            for (let i = 0; i < list.length; i++) {
              list[i].Name = list[i].Name.substr(0, 12);
              if(list[i].StoreCid > 0){
                if (list[i].StoreCid == 48917) {
                  other = list[i];
                } else {
                  newList.push(list[i])
                }
              }
            }
            newList = newList.concat(other);
            that.setData({
              classList: newList
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false,
          })
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getClassList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})